﻿using System;
using GC_VKMD.MusicDownloaderClient.Abstractions.Factories;
using GC_VKMD.MusicDownloaderClient.Abstractions.Interfaces;
using GC_VKMD.MusicDownloaderClient.Abstractions.Interfaces.Managers;
using GC_VKMD.MusicDownloaderClient.Abstractions.Interfaces.ViewModels.Parameters;
using GC_VKMD.MusicDownloaderClient.Enums;
using GC_VKMD.MusicDownloaderClient.ViewModels.Parameters;

namespace GC_VKMD.MusicDownloaderClient.Managers
{
	public class WindowManager : IWindowManager
	{
		#region Variables

		private readonly WindowAdapterBaseFactory _windowAdapterFactory;

		#endregion

		#region Constructors

		public WindowManager(WindowAdapterBaseFactory windowAdapterFactory)
		{
			if (windowAdapterFactory == null)
				throw new ArgumentNullException(string.Format("windowAdapterFactory"));
			_windowAdapterFactory = windowAdapterFactory;
		}

		#endregion

		#region IWindowManager Members

		public WindowDialogStatus ShowMusicDownloaderWindow()
		{
			var parameters = new MusicDownloaderSettingsViewModelParameters();
			FillViewModelParameters(parameters);
			var adapter = _windowAdapterFactory.CreateMusicDownloaderWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		public WindowDialogStatus ShowVKAutorizationWindow()
		{
			var parameters = new MusicDownloaderViewModelParameters();
			FillViewModelParameters(parameters);
			var adapter = _windowAdapterFactory.CreateVKAutorizationWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		#endregion

		#region Internal Implementation

		private WindowDialogStatus ShowWindow(IWindow adapter)
		{
			if (adapter == null) return WindowDialogStatus.Cancel;
			adapter.ShowDialog();
			return adapter.WindowDialogStatus;
		}

		private void FillViewModelParameters(IPrimaryViewModelParameters parameters)
		{
			if (parameters == null) return;
			parameters.WindowManager = this;
		}

		#endregion
	}
}
